home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 July: Mac OS SDK / Dev.CD Jul 96 SDK / Dev.CD Jul 96 SDK1.toast / Development Kits (Disc 1) / OpenDoc Development Framework / ODFDev / Draw / Sources / PalFrame.cpp < prev    next >
Encoding:
Text File  |  1996-04-25  |  7.6 KB  |  293 lines  |  [TEXT/MPS ]

  1. //========================================================================================
  2. //
  3. //    File:                PalFrame.cpp
  4. //    Release Version:    $ ODF 1 $
  5. //
  6. //    Author:                Henri Lamiraux
  7. //
  8. //    Copyright:            (c) 1993 - 1996 by Apple Computer, Inc., all rights reserved.
  9. //
  10. //========================================================================================
  11.  
  12. #include "ODFDraw.hpp"
  13.  
  14. #ifndef PALFRAME_H
  15. #include "PalFrame.h"
  16. #endif
  17.  
  18. #ifndef DRAWPART_H
  19. #include "DrawPart.h"
  20. #endif
  21.  
  22. #ifndef UTILS_H
  23. #include "Utils.h"
  24. #endif
  25.  
  26. // ----- Part Layer -----
  27.  
  28. #ifndef FWUTIL_H
  29. #include "FWUtil.h"
  30. #endif
  31.  
  32. #ifndef FWCONTXT_H
  33. #include "FWContxt.h"
  34. #endif
  35.  
  36. // ----- OS Layer -----
  37.  
  38. #ifndef FWWINDOW_H
  39. #include "FWWindow.h"
  40. #endif
  41.  
  42. #ifndef FWEVENT_H
  43. #include "FWEvent.h"
  44. #endif
  45.  
  46. #ifndef FWLINSHP_H
  47. #include "FWLinShp.h"
  48. #endif
  49.  
  50. #ifndef FWRECT_H
  51. #include "FWRect.h"
  52. #endif
  53.  
  54. #ifndef FWRECSHP_H
  55. #include "FWRecShp.h"
  56. #endif
  57.  
  58. #ifndef FWMEMMGR_H
  59. #include "FWMemMgr.h"
  60. #endif
  61.  
  62. //========================================================================================
  63. // Runtime Information
  64. //========================================================================================
  65.  
  66. #ifdef FW_BUILD_MAC
  67. #pragma segment odfdrawframes
  68. #endif
  69.  
  70. FW_DEFINE_AUTO(CPaletteFrame)
  71.     
  72. //========================================================================================
  73. // CLASS CPalette
  74. //========================================================================================
  75.  
  76. //----------------------------------------------------------------------------------------
  77. // CPalette::CPalette
  78. //----------------------------------------------------------------------------------------
  79.  
  80. CPalette::CPalette() :
  81.     fColorTable(NULL)
  82. {
  83. #ifdef FW_BUILD_MAC
  84.     fColorTable = ::GetCTable(8);
  85.     fNumbersOfColors = 256;
  86. #endif
  87.  
  88. #ifdef FW_BUILD_WIN
  89.     HDC hdc = ::GetDC(NULL);
  90. /*    if (::GetDeviceCaps(hdc,  RASTERCAPS) & RC_PALETTE)
  91.     {
  92.         fNumbersOfColors = ::GetDeviceCaps(hdc, SIZEPALETTE);
  93.         fColorTable = new PALETTEENTRY[fNumbersOfColors];
  94.         ::GetSystemPaletteEntries(hdc, 0, fNumbersOfColors, fColorTable);
  95.     }
  96.     else*/
  97.     {
  98.         fNumbersOfColors = 1 << (::GetDeviceCaps(hdc, PLANES) * ::GetDeviceCaps(hdc, BITSPIXEL));
  99.         if (fNumbersOfColors < 16)
  100.             fNumbersOfColors = 16;
  101.         if (fNumbersOfColors > 256)
  102.             fNumbersOfColors = 256;
  103.         
  104.         fColorTable = new PALETTEENTRY[fNumbersOfColors];
  105.         BYTE red, green, blue;
  106.         red = green = blue = 0;
  107.         
  108.         BYTE redInc, greenInc, blueInc;
  109.         if (fNumbersOfColors == 16)
  110.         {
  111.             redInc = 64;
  112.             greenInc = 128;
  113.             blueInc = 128;        
  114.         }
  115.         else
  116.         {
  117.             redInc = 32;
  118.             greenInc = 32;
  119.             blueInc = 64;        
  120.         }
  121.         
  122.         for (short i = 0; i < fNumbersOfColors; i++)
  123.         {
  124.             fColorTable[i].peRed = red;
  125.             fColorTable[i].peGreen = green;
  126.             fColorTable[i].peBlue = blue;            
  127.             fColorTable[i].peFlags = 0;
  128.             if (!(blue += blueInc))
  129.                 if (!(red += redInc))
  130.                     green += greenInc;
  131. /*
  132.             if (!(red += redInc))
  133.                 if (!(green += greenInc))
  134.                     blue += blueInc;
  135. */
  136.         }
  137.         fColorTable[fNumbersOfColors-1].peRed = 255;
  138.         fColorTable[fNumbersOfColors-1].peGreen = 255;
  139.         fColorTable[fNumbersOfColors-1].peBlue = 255;            
  140.     }
  141.     ::ReleaseDC(NULL, hdc);
  142. #endif
  143. }
  144.  
  145. //----------------------------------------------------------------------------------------
  146. // CPalette::~CPalette
  147. //----------------------------------------------------------------------------------------
  148.  
  149. CPalette::~CPalette()
  150. {
  151.     if (fColorTable)
  152. #ifdef FW_BUILD_MAC
  153.         ::DisposeCTable(fColorTable);
  154. #endif
  155. #ifdef FW_BUILD_WIN
  156.         delete[] fColorTable;
  157. #endif    
  158.     fColorTable = NULL;
  159. }
  160.  
  161. //----------------------------------------------------------------------------------------
  162. // CPalette::GetColor
  163. //----------------------------------------------------------------------------------------
  164.  
  165. void CPalette::GetColor(short colorIndex, FW_CColor* color) const
  166. {
  167. #ifdef FW_BUILD_MAC
  168.     *color = (*fColorTable)->ctTable[colorIndex].rgb;
  169. #endif
  170. #ifdef FW_BUILD_WIN
  171.     if (colorIndex < fNumbersOfColors)
  172.         (*color).SetRGB(fColorTable[colorIndex].peRed,
  173.                         fColorTable[colorIndex].peGreen,
  174.                         fColorTable[colorIndex].peBlue);
  175.     else
  176.         (*color).SetRGB(128, 128, 128);
  177. #endif
  178. }
  179.  
  180. //========================================================================================
  181. // CLASS CPaletteFrame
  182. //========================================================================================
  183.  
  184. //----------------------------------------------------------------------------------------
  185. // CPaletteFrame::CPaletteFrame
  186. //----------------------------------------------------------------------------------------
  187.  
  188. CPaletteFrame::CPaletteFrame(Environment* ev, ODFrame* odFrame, FW_CPresentation* presentation, CDrawPart *drawPart) :
  189.     CFloatingWindowFrame(ev, odFrame, presentation, drawPart),
  190.     fPalette(NULL),
  191.     fGrid(NULL)
  192. {
  193.     fPalette = new CPalette;
  194.     if (fPalette->NumberOfColors() == 256)
  195.     {
  196.         fGrid = new CGrid(8, 32,                     // 8 rows 32 columns
  197.                         FW_CPoint(FW_IntToFixed(2), FW_IntToFixed(2)),
  198.                         FW_CPoint(FW_IntToFixed(8), FW_IntToFixed(8)),
  199.                         FW_kFixedPos1);
  200.     }
  201.     else
  202.     {
  203.         fGrid = new CGrid(2, 8,                     // 2 rows 8 columns
  204.                         FW_CPoint(FW_IntToFixed(2), FW_IntToFixed(2)),
  205.                         FW_CPoint(FW_IntToFixed(16), FW_IntToFixed(16)),
  206.                         FW_kFixedPos1);
  207.     }
  208. }
  209.  
  210. //----------------------------------------------------------------------------------------
  211. // CPaletteFrame::~CPaletteFrame
  212. //----------------------------------------------------------------------------------------
  213.  
  214. CPaletteFrame::~CPaletteFrame()
  215. {
  216.     delete fPalette;
  217.     fPalette = NULL;
  218.     
  219.     delete fGrid;
  220.     fGrid = NULL;
  221. }
  222.  
  223. //----------------------------------------------------------------------------------------
  224. // CPaletteFrame::Draw
  225. //----------------------------------------------------------------------------------------
  226.  
  227. void CPaletteFrame::Draw(Environment* ev, ODFacet* odFacet, ODShape* invalidShape)
  228. {    
  229.     FW_CViewContext vc(ev, this, odFacet, invalidShape);
  230.     vc.SetMapping(fMapping);
  231.  
  232.     EraseBackground(ev, vc);
  233.     
  234.     // ----- Draw the grid -----
  235.     fGrid->DrawGridBorders(vc);
  236.     
  237.     // ----- Draw each color -----
  238.     FW_CColor color;
  239.     FW_CRect rect;
  240.     FW_CRectShape rectShape(FW_kZeroRect, FW_kFill);
  241.     for (unsigned long index = 0; index < fPalette->NumberOfColors(); index++)
  242.     {
  243.         fGrid->GetCellInterior(index, rect);
  244.         rectShape.SetRectangle(rect);
  245.         fPalette->GetColor(index, &color);
  246.         rectShape.GetInk().SetForeColor(color);
  247.         rectShape.Render(vc);
  248.     }
  249. }
  250.  
  251. //----------------------------------------------------------------------------------------
  252. // CPaletteFrame::FacetAdded
  253. //----------------------------------------------------------------------------------------
  254. //    When ODWindow moves back to ODFrame we will be able to move this code
  255. //    back to the constructor
  256.  
  257. void CPaletteFrame::FacetAdded(Environment* ev, ODFacet* facet, unsigned short facetCount)
  258. {
  259.     // ----- Call inherited first -----
  260.     CFloatingWindowFrame::FacetAdded(ev, facet, facetCount);
  261.     
  262.     // ----- Resize my Window -----
  263.     FW_CRect gridRect;
  264.     fGrid->GetExteriorGridRect(gridRect);
  265.     gridRect.Inset(-FW_IntToFixed(2), -FW_IntToFixed(2));
  266.     FW_CPoint windowSize(gridRect.Width(), gridRect.Height());
  267.     GetWindow(ev)->SetWindowSize(ev, windowSize);
  268. }
  269.  
  270. //----------------------------------------------------------------------------------------
  271. // CPaletteFrame::DoMouseDown
  272. //----------------------------------------------------------------------------------------
  273.  
  274. FW_Boolean CPaletteFrame::DoMouseDown(Environment* ev, const FW_CMouseEvent& theMouseEvent)
  275. {
  276.     FW_CPoint where = theMouseEvent.GetLogicalMousePosition(ev, fMapping);
  277.     
  278.     unsigned long colorIndex;    
  279.     if (fGrid->FindCell(where, colorIndex))
  280.     {
  281.         FW_CColor color;
  282.         fPalette->GetColor(colorIndex, &color);
  283.         
  284.         if (theMouseEvent.IsCopyModifier(ev))
  285.             fDrawPart->SetFrameColor(ev, color);
  286.         else
  287.             fDrawPart->SetFillColor(ev, color);    
  288.     }
  289.  
  290.     return TRUE;
  291. }
  292.  
  293.